package com.dtf.base.demo.service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.dtf.core.common.dao.mysql.BaseDaoSupportForMysql;
import com.dtf.core.common.service.BaseService;
import com.dtf.core.common.utils.Const;
import com.dtf.core.common.utils.UUIDGenerator;
import com.dtf.core.common.vo.Pagination;

/**
 * 访问dao层的方式目前有三种
 * 1：继承baseService，可用super获取到Spring 的 jdbcTemplt工具类
 * 2:从super获取BaseDaoSupport获取到super.getDaoSupport().getNamedParameterJdbcTemplate()提供Spring内部封装的工具类
 * 3：从super获取BaseDaoSupport，系统内部自己对两个工具类进行的二次封装，主要用于分页查询等功能
 * @author Administrator
 *
 */
@Service
public class DemoService extends BaseService{
	
	private static Logger log = Logger.getLogger(DemoService.class);
	
	public List queryList(Map param){
		String sql = "select * from demo where 1=1 ";
		
		List sqlParam = new ArrayList();
		if (param != null && Const.containStrValue(param, "demo_name")) {
			sql += " and demo_name=? ";
			sqlParam.add(Const.getStrValue(param, "demo_name"));
		}
		List req =super.getJdbcTemplate().queryForList(sql,sqlParam.toArray(new String[sqlParam.size()]));
		return req;
	}
	
	public Pagination queryPage(Map param){
		List sqlParam = new ArrayList();
		String sql = "select * from demo a ";
		Pagination req = super.getDaoSupport().queryListMapForPage(sql, sqlParam, Const.getPageSize(param), Const.getPageIndex(param));
		return req;
	}
	
	@Transactional
	public void save(Map param) throws Exception{
		
		String sql = "insert into demo (demo_id, demo_name) " +
				"values(?, ?)";
		String id = Const.getUUID();
		List sqlparam = new ArrayList();
		sqlparam.add(id);
		sqlparam.add(Const.getStrValue(param, "demo_name"));
		int i =super.getDaoSupport().update(sql, sqlparam);
//		super.getDaoSupport().getNamedParameterJdbcTemplate().
		log.debug(i);
		//如果需要事务回滚，使用RuntimeExcep tion 进行回滚，Exception回滚无效
//		throw new RuntimeException("aaaa");
	}
}
